package com.cvte.vdt.btcontrolui;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.cvte.tv.api.TvApiSDKManager;
import com.cvte.tv.api.TvServiceConnectListener;
import com.cvte.tv.api.aidl.EntityInputSource;
import com.cvte.tv.api.aidl.EnumInputSourceCategory;
import com.cvte.tv.api.aidl.ITVApiSystemInputSourceAidl;
import com.cvte.tv.api.aidl.ITVApiSystemSignalAidl;
import com.cvte.tv.api.aidl.ITvApiManager;
import com.cvte.vdt.btcontrolui.util.DataUtil;
import com.cvte.vdt.btcontrolui.util.LogUtil;
import com.cvte.vdt.btcontrolui.view.widget.Indicator;
import com.cvte.vdt.btcontrolui.view.widget.MarqueeTextView;
import com.cvte.vdt.uartservice.aidl.IVDTUartBluetoothAidl;
import com.cvte.vdt.uartservice.aidl.IVDTUartCallback;
import com.cvte.vdtlibrary.bluetooth.EntityBluetoothDevice;
import com.cvte.vdtlibrary.bluetooth.EnumBTRecModeStatus;
import com.cvte.vdtlibrary.bluetooth.EnumBTSendModeStatus;
import com.cvte.vdtlibrary.bluetooth.EnumBluetoothKey;
import com.cvte.vdtlibrary.bluetooth.EnumBluetoothMode;
import com.mediatek.twoworlds.tv.MtkTvConfig;
import com.mediatek.twoworlds.tv.MtkTvInputSource;

import java.util.List;

/**
 * Created by user on 2019/1/2.
 */
public class BtRecModeActivity extends Activity implements View.OnClickListener{
    private final static String TAG = "BtRecModeActivity";
    private final static int MSG_UPDATE_PLAY_PAUSE_BTN = 0;
    private final static int MSG_UPDATE_MUSIC_NAME = 1;
    private final static int MSG_UPDATE_BT_NAME = 2;
    private final static int MSG_UPDATE_BT_STATUS =3;
    private final static int MSG_UPDATE_VERSION = 4;
    public static final String NULL_STR = "";
    private Context context;
    private IVDTUartBluetoothAidl ivdtUartBluetoothAidl = null;
    private ITVApiSystemInputSourceAidl mSourceApi = null;
    private ITVApiSystemSignalAidl mSignalApi = null;
    private EntityInputSource lastSource;
    private boolean lastNosignalPoweroff;
    private String strMusicName;
    private String strBtName;
    private String strBtVer;
    private EnumBTRecModeStatus curStatus;
    private ImageButton btnPlayPause;
    private ImageButton btnPre;
    private ImageButton btnNext;
    private TextView textPlayPause;
    private MarqueeTextView btName;
    private MarqueeTextView musicName;
    private TextView textVersion;
    private ImageView ivRightBt;
    private ImageView ivLeftBt;
    private Indicator indicator;
    private boolean isPlaying;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case MSG_UPDATE_PLAY_PAUSE_BTN:
                    if(isPlaying){
                        btnPlayPause.setImageResource(R.drawable.bt_rec_music_play);
                        textPlayPause.setText(R.string.btn_bt_rec_pause);
                        indicator.startAnim();
                    }else {
                        btnPlayPause.setImageResource(R.drawable.bt_rec_music_pause);
                        textPlayPause.setText(R.string.btn_bt_rec_play);
                        indicator.stopAnim();
                    }
                    break;
                case MSG_UPDATE_MUSIC_NAME:
                    musicName.setText(strMusicName);
                    break;
                case MSG_UPDATE_BT_NAME:
                    btName.setText(strBtName);
                    break;
                case MSG_UPDATE_BT_STATUS:
                    if(curStatus == EnumBTRecModeStatus.STATUS_A2DP_CONNECTED){
                        ivRightBt.setImageResource(R.drawable.bluetooth_rec_connect);
                        ivLeftBt.setImageResource(R.drawable.bluetooth_rec_connect);
                    }else if(curStatus == EnumBTRecModeStatus.STATUS_A2DP_DISCON){
                        ivRightBt.setImageResource(R.drawable.bluetooth_rec_disconnect);
                        ivLeftBt.setImageResource(R.drawable.bluetooth_rec_disconnect);
                    }
                    break;
                case MSG_UPDATE_VERSION:
                    textVersion.setText(strBtVer);
                    break;
            }
        }
    };
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            ivdtUartBluetoothAidl = IVDTUartBluetoothAidl.Stub.asInterface(service);
            try {
                ivdtUartBluetoothAidl.registerVDTUartCallback(mCallback);
                ivdtUartBluetoothAidl.setBluetoothMode(EnumBluetoothMode.REC_MODE);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            ivdtUartBluetoothAidl = null;
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        curStatus = EnumBTRecModeStatus.STATUS_A2DP_DISCON;
        isPlaying = false;
        setContentView(R.layout.activity_bt_rec_mode);
        btnPlayPause = (ImageButton)findViewById(R.id.btn_bt_rec_play_pause);
        btnNext = (ImageButton)findViewById(R.id.btn_bt_rec_next);
        btnPre = (ImageButton)findViewById(R.id.btn_bt_rec_pre);
        btnPlayPause.setOnClickListener(this);
        btnPre.setOnClickListener(this);
        btnNext.setOnClickListener(this);

        textPlayPause = (TextView)findViewById(R.id.text_bt_rec_play_pause);

        btName = (MarqueeTextView)findViewById(R.id.text_bt_name);
        musicName = (MarqueeTextView)findViewById(R.id.text_music_name);
        strBtName = context.getString(R.string.rec_bt_no_device);
        strMusicName = NULL_STR;
        btName.setText(R.string.rec_bt_no_device);
        musicName.setText(strMusicName);

        ivLeftBt = (ImageView)findViewById(R.id.iv_left_bt);
        ivRightBt = (ImageView)findViewById(R.id.iv_right_bt);
        textVersion = (TextView)findViewById(R.id.text_version);
        strBtVer = NULL_STR;

        indicator = (Indicator)findViewById(R.id.indicator);
        indicator.stopAnim();

        btnPlayPause.requestFocus();

        Intent serviceIntent = new Intent();
        serviceIntent.setAction(DataUtil.BT_UART_SERVICE_ACTION);
        ComponentName component = new ComponentName(DataUtil.BT_UART_SERVICE_PACKAGE, DataUtil.BT_UART_SERVICE_FULL_NAME);
        serviceIntent.setComponent(component);
        context.bindService(serviceIntent,connection,BIND_AUTO_CREATE);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        TvApiSDKManager.getInstance().getTvApi(new TvServiceConnectListener() {
//            @Override
//            public void OnConnected(ITvApiManager iTvApiManager) {
//                try {
//                    mSourceApi = iTvApiManager.getTVApiSystemInputSource();
//                    lastSource = mSourceApi.eventSystemInputSourceGetInputSource();
//                    mSignalApi = iTvApiManager.getTVApiSystemSignal();
//                    lastNosignalPoweroff = mSignalApi.eventSystemSignalLostPowerOffIsEnabled();
//                    mSignalApi.eventSystemSignalLostPowerOffSetEnable(false);
//                    int id = 0;
//                    for(EntityInputSource source:mSourceApi.eventSystemInputSourceGetList()){
//                        if(source.category == EnumInputSourceCategory.INPUTSOURCE_PC){
//                            id = source.id;
//                        }
//                    }
//                    mSourceApi.eventSystemInputSourceSetInputSource(id);
//                    MtkTvConfig.getInstance().setAudEnable(true);
//                } catch (RemoteException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
    }

    @Override
    protected void onPause() {
//        if(mSourceApi != null){
//            try {
//                MtkTvConfig.getInstance().setAudEnable(false);
//                mSourceApi.eventSystemInputSourceSetInputSource(lastSource.id);
//                mSignalApi.eventSystemSignalLostPowerOffSetEnable(lastNosignalPoweroff);
//            } catch (RemoteException e) {
//                e.printStackTrace();
//            }
//            mSourceApi = null;
//        }
        try {
            if(ivdtUartBluetoothAidl != null) {
                ivdtUartBluetoothAidl.setBluetoothMode(EnumBluetoothMode.SEND_MODE);
                ivdtUartBluetoothAidl.unregisterVDTUartCallback(mCallback);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        context.unbindService(connection);
        finish();
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    private IVDTUartCallback mCallback = new IVDTUartCallback.Stub() {
        @Override
        public void onBluetoothSendModeStatusChange(EnumBTSendModeStatus enumBTSendModeStatus, EntityBluetoothDevice entityBluetoothDevice, List<EntityBluetoothDevice> list) throws RemoteException {

        }

        @Override
        public void onBluetoothSendModeKey(EnumBluetoothKey enumBluetoothKey) throws RemoteException {

        }

        @Override
        public void onBluetoothSendDataChange(List<EntityBluetoothDevice> list) throws RemoteException {

        }

        @Override
        public void onBluetoothRecModeStatusChange(EnumBTRecModeStatus enumBTRecModeStatus, EntityBluetoothDevice entityBluetoothDevice, List<EntityBluetoothDevice> list) throws RemoteException {
            switch (enumBTRecModeStatus){
                case STATUS_A2DP_CONNECTED:
                case STATUS_A2DP_READY:
                    isPlaying = false;
                    handler.sendEmptyMessage(MSG_UPDATE_PLAY_PAUSE_BTN);
                    break;
                case STATUS_A2DP_STREAMING:
                    handler.sendEmptyMessage(MSG_UPDATE_PLAY_PAUSE_BTN);
                    isPlaying = true;
                    break;
            }
        }

        @Override
        public void onBluetoothRecMusicNameChange(String s) throws RemoteException {
            strMusicName = s;
            handler.sendEmptyMessage(MSG_UPDATE_MUSIC_NAME);
        }

        @Override
        public void onBluetoothRecConnectedNameChange(String s) throws RemoteException {
            if(s.equals(NULL_STR)) {
                if(curStatus != EnumBTRecModeStatus.STATUS_A2DP_DISCON) {
                    curStatus = EnumBTRecModeStatus.STATUS_A2DP_DISCON;
                    handler.sendEmptyMessage(MSG_UPDATE_BT_STATUS);
                }
                strBtName = context.getString(R.string.rec_bt_no_device);
            }else {
                if(curStatus != EnumBTRecModeStatus.STATUS_A2DP_CONNECTED) {
                    curStatus = EnumBTRecModeStatus.STATUS_A2DP_CONNECTED;
                    handler.sendEmptyMessage(MSG_UPDATE_BT_STATUS);
                }
                strBtName = s;
            }
            handler.sendEmptyMessage(MSG_UPDATE_BT_NAME);
        }

        @Override
        public void onGetBtVersion(EnumBluetoothMode enumBluetoothMode, String s) throws RemoteException {
            if(enumBluetoothMode == EnumBluetoothMode.REC_MODE){
                strBtVer = s;
                handler.sendEmptyMessage(MSG_UPDATE_VERSION);
            }
        }

        @Override
        public void onGetBtVolume(EnumBluetoothMode enumBluetoothMode, int i) throws RemoteException {

        }
    };

    @Override
    public void onClick(View v) {
        EnumBluetoothKey key = null;
        switch (v.getId()){
            case R.id.btn_bt_rec_play_pause:
                key = EnumBluetoothKey.KEY_PLAY_PAUSE;
                break;
            case R.id.btn_bt_rec_next:
                key = EnumBluetoothKey.KEY_NEXT;
                break;
            case R.id.btn_bt_rec_pre:
                key = EnumBluetoothKey.KEY_PREV;
                break;
        }
        if(ivdtUartBluetoothAidl != null && key != null){
            try {
                ivdtUartBluetoothAidl.sendBTRecModeKey(key);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }
}
